import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/data/RechargeRecordEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class rechargeRecordController extends BaseController {
  RxList<ListRechargeElement> recharge1List = <ListRechargeElement>[].obs;
  RxList<ListRechargeElement> recharge2List = <ListRechargeElement>[].obs;
  RxList<ListRechargeElement> recharge3List = <ListRechargeElement>[].obs;
  RxList<ListRechargeElement> recharge4List = <ListRechargeElement>[].obs;

  var page = 1;

  void refreshData(RefreshController refreshController,int state) {
    page=1;
    reqRechargeList(refreshController,state);
  }


  void loadData(RefreshController refreshController,int state) {
    page+=1;
    reqRechargeList(refreshController,state);
  }

  RxList<ListRechargeElement> getList(int state){
    var stateArray = [-1, 0, 1, 2];
    var listArray = [recharge1List,recharge2List,recharge3List,recharge4List];
    return listArray[stateArray.indexOf(state)];
  }

  void reqRechargeList(RefreshController refreshController, int state) async {
    if(page==1){
      showLoading();
    }
    var resp = await HttpManager.reqRechargeRecord(page, state);
    if (resp.isSuccessData()) {
      if(page==1){
        getList(state).assignAll(resp.data!.list);
        refreshController.refreshCompleted();
      }else{
        getList(state).addAll(resp.data!.list);
        if(resp.data!.list.isEmpty){
          refreshController.loadNoData();
        }else{
          refreshController.loadComplete();
        }

      }
    }
    hideLoading();
  }
}
